package com.du.dupload;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.du.dupload.SwingStatusButton.State;

public class SwingItemProgress extends JPanel implements IUICallback {
	private JLabel labelName;
	private JLabel labelStatus;
	private JLabel labelInfo;
	private JProgressBar pb;
	private SwingStatusButton button;

	SwingItemProgress() {
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		setPreferredSize(new Dimension(600, 35));

		button = new SwingStatusButton();
		button.setPreferredSize(new Dimension(40, 20));
		add(button);

		labelName = new JLabel("Name");
		labelName.setPreferredSize(new Dimension(150, 25));
		add(labelName);

		labelStatus = new JLabel("Status");
		labelStatus.setPreferredSize(new Dimension(80, 25));
		add(labelStatus);

		pb = new JProgressBar();
		pb.setMinimum(0);
		pb.setMaximum(100);
		pb.setStringPainted(true);
		pb.setPreferredSize(new Dimension(150, 25));
		add(pb);

		labelInfo = new JLabel("Info");
		labelInfo.setPreferredSize(new Dimension(80, 25));
		add(labelInfo);

	}

	@Override
	public void setInfo(String text) {
		labelInfo.setText(text);

	}

	@Override
	public void setStatus(String text) {
		labelStatus.setText(text);

	}

	@Override
	public void setProgress(float progressPercent) {
		pb.setValue(Math.round(progressPercent));
		pb.setString(Math.round(progressPercent * 10.) / 10. + "%");

	}

	@Override
	public void startLoading(String name) {
		if (name != "") {
			labelStatus.setText(name);
		}
		button.changeState(State.START);

	}

	@Override
	public void stopLoading() {
		labelStatus.setText("Stopped");
		button.changeState(State.STOP);

	}

	@Override
	public void showError(String message) {
		labelStatus.setText("Error");

	}

	@Override
	public void setItemName(String text) {
		labelName.setText(text);

	}

	@Override
	public void setCallback(IUICallbackHandler ch) {
		button.setHandler(ch);

	}

	@Override
	public void setFinished() {
		labelStatus.setText("Finished");
		button.changeState(State.FINISH);
		
	}
}
